using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using THSERP.BusinessEntities;
using THSERP.BaseProvider;
namespace THSERP.Modules.Project
{
    public class PMProjectPhasesGridControl : THSGridControl
    {
        public override void InitGridControlDataSource()
        {
            ProjectEntities entity = (ProjectEntities)((BaseModuleERP)this.Screen.Module).CurrentModuleEntity;
            BindingSource bds = new BindingSource();
            bds.DataSource = entity.PMProjectPhaseItemsEntityList;
            this.DataSource = bds;
        }

        protected override DevExpress.XtraGrid.Views.Grid.GridView InitializeGridView()
        {
            DevExpress.XtraGrid.Views.Grid.GridView gridView = base.InitializeGridView();
            gridView.FocusedRowChanged += new DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventHandler(GridView_FocusedRowChanged);
            return gridView;
        }

        protected override void GridView_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            base.GridView_FocusedRowChanged(sender, e);

            if (e.PrevFocusedRowHandle >= 0)
            {
                DevExpress.XtraGrid.Views.Grid.GridView gridView = (DevExpress.XtraGrid.Views.Grid.GridView)this.MainView;
                ProjectEntities entity = (ProjectEntities)((BaseModuleERP)Screen.Module).CurrentModuleEntity;
                int iIndex = gridView.GetDataSourceRowIndex(e.PrevFocusedRowHandle);

                if (iIndex >= 0)
                {
                    ProjectPhaseItemsEntity entItem = entity.PMProjectPhaseItemsEntityList[iIndex];
                    entItem.PMProjectPhaseResourcesList.Clear();
                    foreach (PMProjectPhaseResourcesInfo objProjectPhaseResourcesInfo in entity.PMProjectPhaseResourceEmployeesList)
                        entItem.PMProjectPhaseResourcesList.Add((PMProjectPhaseResourcesInfo)objProjectPhaseResourcesInfo.Clone());
                    foreach (PMProjectPhaseResourcesInfo objProjectPhaseResourcesInfo in entity.PMProjectPhaseResourceEquipmentsList)
                        entItem.PMProjectPhaseResourcesList.Add((PMProjectPhaseResourcesInfo)objProjectPhaseResourcesInfo.Clone());
                    foreach (PMProjectPhaseResourcesInfo objProjectPhaseResourcesInfo in entity.PMProjectPhaseResourceMaterialsList)
                        entItem.PMProjectPhaseResourcesList.Add((PMProjectPhaseResourcesInfo)objProjectPhaseResourcesInfo.Clone());
                }
            }
        }
    }
}
